package com.atguigu.springcloud.Ib;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author csc
 * @Date 2023/3/15 0015 13:49
 * @Version 1.0
 */
@Component
public class MyLb implements LoadBalancer
{

     private AtomicInteger atomicInteger = new AtomicInteger(0);

    @Override
    public ServiceInstance instances(List<ServiceInstance> serviceInstances) {

        int index = getAndIntcrement() % serviceInstances.size();
        return serviceInstances.get(index);
    }//end

     public final int getAndIntcrement() {
          int current;
          int next;
          do {
              current= this.atomicInteger.get();
              //三目运算  请求次数超过整数最大值 就从0 开始
              next = current >=Integer.MAX_VALUE ? 0 : current +1;
          }while (!this.atomicInteger.compareAndSet(current,next));
         System.out.println("**********第几次范文,次数next:"+ next);
         return next;
     }

    public static void main(String[] args) {
        BigDecimal d1 = new BigDecimal(5);
        BigDecimal d2 = new BigDecimal(6);
        System.out.println(d1.add(d2));
    }


}
